package jsp.estore.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.print.attribute.standard.Fidelity;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jsp.estore.domain.Product;
import jsp.estore.factory.ProductServiceFactory;
import jsp.estore.service.ProductService;
import jsp.estore.service.ProductServiceImpl;
import jsp.estore.utils.PicUtils;
import jsp.estore.utils.UploadUtils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.gson.Gson;

public class ProductServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		

		if ("findById".equals(method)) {
			// 根据id查找商品
			findById(request, response);
		} else if ("findAll".equals(method) || method == null) {
			// 查找全部商品
			findAll(request, response);
		
		}
	}

	// 根据id查找商品
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.得到要查询的商品的id
		String id = request.getParameter("id");


		// 2.调用service中查询方法
		try {
			ProductService service = ProductServiceFactory.getInstance();
			Product p = service.findById(id);

			request.setAttribute("p", p);
			request.getRequestDispatcher("/productInfo.jsp").forward(request, response);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 查找所有商品
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//搜索参数
			String search = request.getParameter("search").trim();
			String key = request.getParameter("key").trim();
			if(search==null){
				search = "";
			}
			int start =Integer.parseInt( request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			
			ProductService service = ProductServiceFactory.getInstance();
			List<Product> ps = service.findAll(search,start,length,key);
			
			Gson gson = new Gson();
			String result = gson.toJson(ps);
			
			PrintWriter out = response.getWriter();
			out.print(result);
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
